package com.wlient.springboot_stomp.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;


/**
 * @Author JCccc
 * @Description   EnableWebSocketMessageBroker-注解开启STOMP协议来传输基于代理的消息，此时控制器支持使用@MessageMapping
 * @Date 2021/6/30 8:53
 */

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

  @Override
  public void configureMessageBroker(MessageBrokerRegistry config) {

    //topic用来广播，user用来实现点对点
    //点对点应配置一个/user消息代理，广播式应配置一个/topic消息代理
    config.enableSimpleBroker("/topic","/user");
    //点对点使用的订阅前缀（客户端订阅路径上会体现出来），不设置的话，默认也是/user/
    config.setUserDestinationPrefix("/user");
  }

  /**
   * 开放节点
   * @param registry
   */
  @Override
  public void registerStompEndpoints(StompEndpointRegistry registry) {
    //注册两个STOMP的endpoint，分别用于广播和点对点


    //广播
    registry.addEndpoint("/publicServer").setAllowedOriginPatterns("*").withSockJS();

    //点对点
    registry.addEndpoint("/privateServer").setAllowedOriginPatterns("*").withSockJS();
  }



}